package com.spring_mvc.web.servlet;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.spring_mvc.domain.User;
import com.spring_mvc.domain.VO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.MultipartFilter;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.ws.RequestWrapper;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class MvcServlet {

    /*
        多文件上传
     */
    @RequestMapping("test22")
    @ResponseBody
    public void test22(String name, MultipartFile[] file) throws IOException {
        for (MultipartFile multipartFile : file) {
            if (!multipartFile.isEmpty()){
                String filename = multipartFile.getOriginalFilename();
                multipartFile.transferTo(new File("C:\\Users\\25046\\Desktop\\文件练习\\" + filename));
            }
        }
    }


    /*
        文件上传
     */
    @RequestMapping("test21")
    @ResponseBody
    public void test21(String name, MultipartFile file, MultipartFile file1) throws IOException {
        //获取文件名称
        String filename = file.getOriginalFilename();
        file.transferTo(new File("C:\\Users\\25046\\Desktop\\" + filename));

        //多文件上传
        String filename1 = file.getOriginalFilename();
        file.transferTo(new File("C:\\Users\\25046\\Desktop\\" + filename1));

    }


    /*
        获取请求头(cookie)
     */
    @RequestMapping("test20")
    @ResponseBody
    public void test20(@CookieValue(value = "JSESSIONID", required = false) String jsessionid) {
        System.out.println(jsessionid);
    }


    /*
        获取请求头
     */
    @RequestMapping("test19")
    @ResponseBody
    public void test19(@RequestHeader(value = "Host", required = false) String host) {
        System.out.println(host);
    }


    /*
        获取Servlet的API
     */
    @RequestMapping("test18")
    @ResponseBody
    public void test18(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        System.out.println(request);
        System.out.println(response);
        System.out.println(request.getSession() == session);
    }


    /*
        自定义类型转换器
     */
    @RequestMapping("test17")
    @ResponseBody
    public void test17(Date date) {
        System.out.println(date);
    }


    /*
         PathVariable:用于获取Restful风格的参数(url/参数)
         参数一:要解析的参数名

         GET : 获得资源
         POST : 新建资源
         PUT : 更新资源
         DELETE : 删除资源
     */
    @RequestMapping("test16/{name}")
    @ResponseBody
    public void test16(@PathVariable("name") String username) {
        System.out.println(username);
    }

    /*
        RequestParam参数绑定
        参数一(value):匹配请求参数名称
        参数二(required):如果没有相对应的匹配参数,报错,默认为true
        参数三(defaultValue):默认值,当没有参数时,使用默认值进项赋值
     */
    @RequestMapping("test15")
    @ResponseBody
    public void test15(@RequestParam(value = "name", required = false, defaultValue = "于金彪") String username) {
        System.out.println(username);
    }

    //RequestBody获取集合请求参数(注解),直接进行参数封装
    @RequestMapping("test14")
    @ResponseBody
    public void test14(@RequestBody List<User> userList) {
        System.out.println(userList);
    }


    //获取集合请求参数
    @RequestMapping("test13")
    @ResponseBody
    public void test13(VO vo) {
        System.out.println(vo);
    }

    //获取数组请求参数
    @RequestMapping("test12")
    @ResponseBody
    public void test12(String[] str) {
        System.out.println(Arrays.toString(str));
    }

    //获取POJO请求参数
    @RequestMapping("test11")
    @ResponseBody
    public void test11(User user) {
        System.out.println(user);
    }


    //  获取基本请求参数
    @RequestMapping("test10")
    @ResponseBody
    public void test10(String username, int age) {
        System.out.println(username);
        System.out.println(age);
    }


    //  返回json(简化操作,让springmvc自动将user转换为json格式)
    @RequestMapping(value = "test9")
    @ResponseBody
    public User test9(HttpServletResponse response) throws JsonProcessingException {
        User user = new User();
        user.setUsername("于金彪");
        user.setAge(56);

        return user;
    }


    //  返回json
    @RequestMapping(value = "test8", produces = "text/html;charset=utf-8")  // produces 设置编码,防止乱码
    @ResponseBody
    public String test8(HttpServletResponse response) throws JsonProcessingException {
        User user = new User();
        user.setUsername("于金彪");
        user.setAge(12);

        //使用json转换工具
        ObjectMapper obj = new ObjectMapper();
        String str = obj.writeValueAsString(user);

        return str;
    }


    //  返回json格式字符串
    @RequestMapping("test7")
    @ResponseBody
    public String test7() {
        return "{\"username\":1,\"password\":2,\"age\":3}";
    }


    //  直接回显数据,ResponseBody
    @RequestMapping("test6")
    @ResponseBody()  //告知SpringMVC框架 不进行视图跳转 直接进行视图响应
    public String test6() {
        return "hello world";
    }

    //    HttpServletResponse
    @RequestMapping("test5")
    public void test5(HttpServletResponse response) throws IOException {
        response.getWriter().write(" hello world ");
    }

    //    HTTPServletRequest
    @RequestMapping("test4")
    public String test4(HttpServletRequest request) {

        request.setAttribute("username", "log");

        return "succes";
    }


    //    Model和View分开
    @RequestMapping("test3")
    public String test3(Model model) {

        model.addAttribute("username", "find");

        return "succes";
    }


    //    ModelAndView合并
    @RequestMapping("test2")
    public ModelAndView test2(ModelAndView modelAndView) {

        modelAndView.addObject("username", "rel");

        modelAndView.setViewName("succes");

        return modelAndView;
    }


    //    页面跳转:通过ModelAndView返回
    @RequestMapping("/test1")
    public ModelAndView test1() {
        /*
            Model : 模型 封装数据
            View  : 视图 展示数据ew
         */

        ModelAndView view = new ModelAndView();

        //设置模型数据
        view.addObject("username", "del");

        //设置视图名称
        view.setViewName("succes");

        return view;
    }


    //    页面跳转:返回字符串
    //    @RequestMapping(value = "/add",method = RequestMethod.GET,params = {"username","password"})
    @RequestMapping("/test")
    public String test() {
        System.out.println(" add yes!  ");
        return "succes";   //默认forward(转发地址栏不变)  redirect(重定向地址栏发生变化)
    }

}
